package com.jxdinfo.hussar.example.mes.pro.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.jxdinfo.hussar.common.security.BaseSecurityUtil;
import com.jxdinfo.hussar.example.common.constant.UserConstants;
import com.jxdinfo.hussar.example.common.core.controller.BaseController;
import com.jxdinfo.hussar.example.common.core.domain.AjaxResult;
import com.jxdinfo.hussar.example.common.core.page.TableDataInfo;
import com.jxdinfo.hussar.example.common.enums.BusinessType;
//import org.springframework.security.access.prepost.PreAuthorize;
import com.jxdinfo.hussar.example.common.system.strategy.AutoCodeUtil;
import com.jxdinfo.hussar.example.common.utils.StringUtils;
import com.jxdinfo.hussar.example.mes.pro.domain.*;
import com.jxdinfo.hussar.example.mes.pro.domain.Erp.*;
import com.jxdinfo.hussar.example.mes.pro.service.*;
import com.jxdinfo.hussar.example.mes.qc.domain.QcResult;
import com.jxdinfo.hussar.example.mes.qc.service.IQcResultService;
import com.jxdinfo.hussar.example.mes.wm.domain.*;
import com.jxdinfo.hussar.example.mes.wm.domain.tx.ProductRecptTxBean;
import com.jxdinfo.hussar.example.mes.wm.service.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jxdinfo.hussar.example.common.annotation.Log;
import com.jxdinfo.hussar.example.common.utils.poi.ExcelUtil;

/**
 * 包装入库Controller
 *
 * @author ruoyi
 * @date 2024-04-08
 */
@RestController
@RequestMapping("/mes/pro/recpt")
public class ProProductRecptController extends BaseController
{
    @Autowired
    private IProProductRecptService proProductRecptService;

    @Autowired
    private IWmProductRecptLineService wmProductRecptLineService;

    @Autowired
    private IWmWarehouseService wmWarehouseService;

    @Autowired
    private IProWorkorderService proWorkorderService;

    @Autowired
    private IWmStorageLocationService wmStorageLocationService;

    @Autowired
    private IWmStorageAreaService wmStorageAreaService;

    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Autowired
    private IStorageCoreService storageCoreService;

    @Autowired
    private IErpService erpService;

    @Autowired
    private IProTaskService proTaskService;

    //工艺组成
    @Autowired
    private IProRouteProcessService proRouteProcessService;

    @Autowired
    private IQcResultService qcResultService;

    /**
     * 查询包装入库列表
     */
//    @PreAuthorize("@ss.hasPermi('pro:recpt:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProProductRecpt proProductRecpt)
    {
        startPage();
        List<ProProductRecpt> list = proProductRecptService.selectProProductRecptList(proProductRecpt);
        return getDataTable(list);
    }

    /**
     * 导出包装入库列表
     */
//    @PreAuthorize("@ss.hasPermi('pro:recpt:export')")
    @Log(title = "包装入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProProductRecpt proProductRecpt)
    {
        List<ProProductRecpt> list = proProductRecptService.selectProProductRecptList(proProductRecpt);
        ExcelUtil<ProProductRecpt> util = new ExcelUtil<ProProductRecpt>(ProProductRecpt.class);
        util.exportExcel(response, list, "包装入库数据");
    }

    /**
     * 获取包装入库详细信息
     */
//    @PreAuthorize("@ss.hasPermi('pro:recpt:query')")
    @GetMapping(value = "/get/{recptId}")
    public AjaxResult getInfo(@PathVariable("recptId") Long recptId)
    {
        return AjaxResult.success(proProductRecptService.selectProProductRecptByRecptId(recptId));
    }

    /**
     * 新增包装入库
     */
//    @PreAuthorize("@ss.hasPermi('pro:recpt:add')")
    @Log(title = "包装入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProProductRecpt proProductRecpt)
    {

        proProductRecpt.setQuantityRecpt(proProductRecpt.getRealBags());
        if(UserConstants.NOT_UNIQUE.equals(proProductRecptService.checkUnique(proProductRecpt))){
            return AjaxResult.error("入库单编号已存在！");
        }
        if(StringUtils.isNotNull(proProductRecpt.getWarehouseId())){
            WmWarehouse warehouse = wmWarehouseService.selectWmWarehouseByWarehouseId(proProductRecpt.getWarehouseId());
            proProductRecpt.setWarehouseCode(warehouse.getWarehouseCode());
            proProductRecpt.setWarehouseName(warehouse.getWarehouseName());
        }
//        if(StringUtils.isNotNull(proProductRecpt.getLocationId())){
//            WmStorageLocation location = wmStorageLocationService.selectWmStorageLocationByLocationId(proProductRecpt.getLocationId());
//            proProductRecpt.setLocationCode(location.getLocationCode());
//            proProductRecpt.setLocationName(location.getLocationName());
//        }
//        if(StringUtils.isNotNull(proProductRecpt.getAreaId())){
//            WmStorageArea area = wmStorageAreaService.selectWmStorageAreaByAreaId(proProductRecpt.getAreaId());
//            proProductRecpt.setAreaCode(area.getAreaCode());
//            proProductRecpt.setAreaName(area.getAreaName());
//        }
        //根据生产任务id得到生产任务
        ProTask task = proTaskService.selectProTaskByTaskId(proProductRecpt.getTaskId());
        proProductRecpt.setProcessId(task.getProcessId());
        proProductRecpt.setProcessCode(task.getProcessCode());
        proProductRecpt.setProcessName(task.getProcessName());
        //根据生产任务得到该生产任务的工艺组成（主要得到他是否减订单keyFlag）
        ProRouteProcess routeProcess = new ProRouteProcess();
        routeProcess.setRouteId(task.getRouteId());
        routeProcess.setProcessId(task.getProcessId());
        List<ProRouteProcess> proRouteProcesses = proRouteProcessService.selectProRouteProcessList(routeProcess);
        //根据生产订单号和生产任务号检查对应的工艺路线和工序配置是否存在
        if(proRouteProcesses.size() == 0){
            return AjaxResult.error("未找到生产任务对应的工艺工序配置！");
        }else {
            routeProcess = proRouteProcesses.get(0);
            //是否减订单
            proProductRecpt.setKeyFlag(routeProcess.getKeyFlag());
        }
        proProductRecpt.setCreateBy(BaseSecurityUtil.getUser().getUserName());
        int i = proProductRecptService.insertProProductRecpt(proProductRecpt);

        //如果是否需要检化验 isCheck=1 则需要检化验,qc_result生成一条检化验记录
        if(UserConstants.YES.equals(proProductRecpt.getIsCheck())){
            try
            {
                QcResult qcResult = new QcResult();
                BeanUtils.copyProperties(qcResult,proProductRecpt);
                qcResult.setQuantityFeedback(proProductRecpt.getQuantityRecpt());
                qcResult.setQuantityUncheck(proProductRecpt.getQuantityRecpt());
//                qcResult.setUserName(proFeedback.getNickName());
                qcResult.setRecordCode(autoCodeUtil.genSerialCode(UserConstants.RECORD_CODE,""));
                qcResult.setIsComplete("0");
                qcResult.setFeedbackId(proProductRecpt.getRecptId());
                qcResult.setFeedbackCode(proProductRecpt.getRecptCode());
                qcResultService.insertQcResult(qcResult);
            }
            catch (Exception e)
            {
                return AjaxResult.error("生成检化验记录失败！");
            }
        }else {
//            if(!StringUtils.isNotNull(proProductRecpt.getQuantityQualified()) || !StringUtils.isNotNull(proProductRecpt.getQuantityUnquanlified())){
//                return AjaxResult.error("请输入合格品和不良品数量！");
//            }
            if(!StringUtils.isNotNull(proProductRecpt.getRealBags())){
                return AjaxResult.error("请输入实际包装数量！");
            }
        }
        return toAjax(i);
    }

    /**
     * 修改包装入库
     */
//    @PreAuthorize("@ss.hasPermi('pro:recpt:edit')")
    @Log(title = "包装入库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProProductRecpt proProductRecpt)
    {
        proProductRecpt.setQuantityRecpt(proProductRecpt.getRealBags());
        proProductRecpt.setQuantityQualified(proProductRecpt.getRealBags());
        if(UserConstants.NOT_UNIQUE.equals(proProductRecptService.checkUnique(proProductRecpt))){
            return AjaxResult.error("入库单编号已存在！");
        }


        if(StringUtils.isNotNull(proProductRecpt.getWarehouseId())){
            WmWarehouse warehouse = wmWarehouseService.selectWmWarehouseByWarehouseId(proProductRecpt.getWarehouseId());
            proProductRecpt.setWarehouseCode(warehouse.getWarehouseCode());
            proProductRecpt.setWarehouseName(warehouse.getWarehouseName());
        }
//        if(StringUtils.isNotNull(proProductRecpt.getLocationId())){
//            WmStorageLocation location = wmStorageLocationService.selectWmStorageLocationByLocationId(proProductRecpt.getLocationId());
//            proProductRecpt.setLocationCode(location.getLocationCode());
//            proProductRecpt.setLocationName(location.getLocationName());
//        }
//        if(StringUtils.isNotNull(proProductRecpt.getAreaId())){
//            WmStorageArea area = wmStorageAreaService.selectWmStorageAreaByAreaId(proProductRecpt.getAreaId());
//            proProductRecpt.setAreaCode(area.getAreaCode());
//            proProductRecpt.setAreaName(area.getAreaName());
//        }

        try
        {
            QcResult qcResult = new QcResult();
            qcResult.setFeedbackId(proProductRecpt.getRecptId());
            qcResult.setFeedbackCode(proProductRecpt.getRecptCode());
            List<QcResult> qcResults = qcResultService.selectQcResultList(qcResult);
            if (qcResults.size() >0){
                qcResult.setRecordId(qcResults.get(0).getRecordId());
                qcResult.setQuantityFeedback(proProductRecpt.getQuantityRecpt());
                qcResult.setQuantityUncheck(proProductRecpt.getQuantityRecpt());
                qcResult.setQuantityQualified(BigDecimal.valueOf(0));
                qcResult.setIsComplete("0");
                qcResult.setStatus(UserConstants.ORDER_STATUS_PREPARE);
                qcResultService.updateQcResult(qcResult);
            }
        }
        catch (Exception e)
        {
            return AjaxResult.error("修改检化验记录失败！");
        }

        //检查对应的工艺路线和工序配置
        if(StringUtils.isNotNull(proProductRecpt.getRouteId())&& StringUtils.isNotNull(proProductRecpt.getProcessId())){
            ProRouteProcess param = new ProRouteProcess();
            param.setRouteId(proProductRecpt.getRouteId());
            param.setProcessId(proProductRecpt.getProcessId());
            List<ProRouteProcess> processes = proRouteProcessService.selectProRouteProcessList(param);
            if(CollectionUtil.isEmpty(processes)){
                return AjaxResult.error("未找到生产任务对应的工艺工序配置！");
            }
        }else {
            return AjaxResult.error("当前生产任务对应的工艺工序配置无效！");
        }

        //检查数量
        if(UserConstants.YES.equals(proProductRecpt.getIsCheck())){
            if(!StringUtils.isNotNull(proProductRecpt.getQuantityUncheck())){
                return AjaxResult.error("当前工段包装需要经过质检确认，请输入待检测数量!");
            }
        }else {
            if(!StringUtils.isNotNull(proProductRecpt.getQuantityQualified()) || !StringUtils.isNotNull(proProductRecpt.getQuantityUnquanlified())){
                return AjaxResult.error("请输入合格品和不良品数量！");
            }
        }
        return toAjax(proProductRecptService.updateProProductRecpt(proProductRecpt));
    }

    /**
     * 删除包装入库
     */
//    @PreAuthorize("@ss.hasPermi('pro:recpt:remove')")
    @Log(title = "包装入库", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{recptIds}")
    public AjaxResult remove(@PathVariable Long[] recptIds)
    {
        for (Long recptId: recptIds
        ) {
            wmProductRecptLineService.deleteByRecptId(recptId);
        }
        return toAjax(proProductRecptService.deleteProProductRecptByRecptIds(recptIds));
    }
    /**
     * 执行入库
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('mes:wm:productrecpt:edit')")
    @Log(title = "产品入库记录", businessType = BusinessType.UPDATE)
    @Transactional
    @GetMapping("/{recptId}")
    public AjaxResult execute(@PathVariable Long recptId)
    {
        if(!StringUtils.isNotNull(recptId)){
            return AjaxResult.error("请先保存包装单据");
        }

        ProProductRecpt proProductRecpt = proProductRecptService.selectProProductRecptByRecptId(recptId);
        proProductRecpt.setQuantityRecpt(proProductRecpt.getRealBags());
        if(proProductRecpt.getQuantityRecpt().compareTo(BigDecimal.ZERO) !=1){
            return AjaxResult.error("包装数量必须大于0");
        }
        //生产工单
        ProWorkorder workorder = proWorkorderService.selectProWorkorderByWorkorderId(proProductRecpt.getWorkorderId());
        //生产任务
        ProTask task = proTaskService.selectProTaskByTaskId(proProductRecpt.getTaskId());

        //判断当前生产任务的状态，如果已经完成则不能再包装
        if(UserConstants.ORDER_STATUS_FINISHED.equals(task.getStatus())){
            return AjaxResult.error("当前生产工单的状态为已完成，不能继续包装，请刷新生产任务列表！");
        }
        QcResult qcResult = new QcResult();
        qcResult.setFeedbackId(proProductRecpt.getRecptId());
        List<QcResult> qcResults = qcResultService.selectQcResultList(qcResult);
        if (qcResults.size() != 0 && qcResults.get(0).getIsComplete().equals("0") ){
            return  AjaxResult.error("当前报工单未完成检验，无法执行报工！");
        }

//        //仍旧有待检数量时不能执行
//        if(StringUtils.isNotNull(proProductRecpt.getQuantityUncheck()) && proProductRecpt.getQuantityUncheck().compareTo(BigDecimal.ZERO) >0){
//            return  AjaxResult.error("当前包装单未完成检验（待检数量大于0），无法执行包装！");
//        }


        //更新生产任务的生产数量
        BigDecimal quantityProduced,quantityQuanlify,quantityUnquanlify;
        quantityQuanlify = task.getQuantityQuanlify()==null? new BigDecimal(0):task.getQuantityQuanlify();
        quantityUnquanlify = task.getQuantityUnquanlify() ==null? new BigDecimal(0):task.getQuantityUnquanlify();
        quantityProduced = task.getQuantityProduced()==null? new BigDecimal(0):task.getQuantityProduced();
        //已生产数量
        task.setQuantityProduced(quantityProduced.add(proProductRecpt.getQuantityRecpt()));
        //不良品数量
        task.setQuantityUnquanlify(quantityUnquanlify.add(proProductRecpt.getQuantityUnquanlified()));
        //合格品数量
        task.setQuantityQuanlify(quantityQuanlify.add(proProductRecpt.getQuantityQualified()));
        proTaskService.updateProTask(task);



        //如果是否减定单为是，则需调用erp完工报告
        if(UserConstants.YES.equals(proProductRecpt.getKeyFlag())){
            //回传erp完工报告
            JSONObject jsonObject = executeProProductRecpt(proProductRecpt);
            DataM dataM = jsonObject.toJavaObject(DataM.class);
            if (dataM.getData().get(0).getM_isSucess()){
                //更新生产工单的生产数量
                if(proRouteProcessService.checkKeyProcess(proProductRecpt)){
                    BigDecimal produced = workorder.getQuantityProduced() == null?new BigDecimal(0):workorder.getQuantityProduced();
                    BigDecimal feedBackQuantity = proProductRecpt.getQuantityRecpt() ==null?new BigDecimal(0):proProductRecpt.getQuantityRecpt();
                    workorder.setQuantityProduced( produced.add(feedBackQuantity));
                    proWorkorderService.updateProWorkorder(workorder);
                }
                //更新包装单的状态
                proProductRecpt.setStatus(UserConstants.ORDER_STATUS_FINISHED);
                return AjaxResult.success(proProductRecptService.updateProProductRecpt(proProductRecpt));
            }else {
                return AjaxResult.error(dataM.getData().get(0).getM_errorMsg());
            }
        }else {
            //更新生产工单的生产数量
            if(proRouteProcessService.checkKeyProcess(proProductRecpt)){
                BigDecimal produced = workorder.getQuantityProduced() == null?new BigDecimal(0):workorder.getQuantityProduced();
                BigDecimal feedBackQuantity = proProductRecpt.getQuantityRecpt() ==null?new BigDecimal(0):proProductRecpt.getQuantityRecpt();
                workorder.setQuantityProduced( produced.add(feedBackQuantity));
                proWorkorderService.updateProWorkorder(workorder);
            }
            //更新包装单的状态
            proProductRecpt.setStatus(UserConstants.ORDER_STATUS_FINISHED);
            return AjaxResult.success(proProductRecptService.updateProProductRecpt(proProductRecpt));
        }


//
//        ProProductRecpt recpt = proProductRecptService.selectProProductRecptByRecptId(recptId);
//
//        WmProductRecptLine param = new WmProductRecptLine();
//        param.setRecptId(recptId);
//        List<WmProductRecptLine> lines = wmProductRecptLineService.selectWmProductRecptLineList(param);
//        if(CollUtil.isEmpty(lines)){
//            return AjaxResult.error("请添加要入库的产品");
//        }
//
////        JSONObject jsonObject = executeProProductRecpt(recpt);
//
//        List<ProductRecptTxBean> beans = proProductRecptService.getTxBean(recptId);
//        storageCoreService.processProductRecpt(beans);
//
//        recpt.setStatus(UserConstants.ORDER_STATUS_FINISHED);
//        proProductRecptService.updateProProductRecpt(recpt);
//
//        return AjaxResult.success();
    }


    /**
     * 同步ERP完工报告
     * @param recpt
     * @return
     */
    private JSONObject executeProProductRecpt(ProProductRecpt recpt){
        CompRptDatas compRptDatas = new CompRptDatas();
        //Other
        compRptDatas.setOtherID(String.valueOf(recpt.getWorkorderId()));  //工单id
        compRptDatas.setDocNo(recpt.getWorkorderCode());  //工单code
        //Org
        Org org = new Org();       // 死数据
        org.setID(1002401113575156L);                                   //类型转换
        org.setCode("001");
        org.setName("山东鲁银新材料科技有限公司");
        compRptDatas.setOrg(org);
        compRptDatas.setDescription("");
        //CompleteDate
        compRptDatas.setCompleteDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(recpt.getRecptDate()));  //完工时间    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
        //CompleteDocType
        CompleteDocType completeDocType = new CompleteDocType();   //完工类型
        completeDocType.setID(1002401113821550L);                  //类型转换
        completeDocType.setCode("1");
        completeDocType.setName("产品完工报告");
        compRptDatas.setCompleteDocType(completeDocType);
        //Direction
        compRptDatas.setDirection(0);
        //HandleDept
        HandleDept handleDept = new HandleDept();
        handleDept.setID(0);                        //经手部门id
        handleDept.setCode("");                     //经手部门编码
        handleDept.setName("");                     //经手部门名称
        compRptDatas.setHandleDept(handleDept);
        //HandlePerson
        HandlePerson handlePerson = new HandlePerson();
        handlePerson.setID(0);                              //经手人id
        handlePerson.setCode(recpt.getUserName());       //经手人编码
        handlePerson.setName(recpt.getNickName());       //经手人名称
        compRptDatas.setHandlePerson(handlePerson);
        //MO
        MO mo = new MO();
        mo.setID(recpt.getWorkorderId());           //订单编号            //类型转换
        mo.setCode(recpt.getWorkorderCode());        //订单编码
        mo.setName(recpt.getWorkorderName());        //订单名称
        compRptDatas.setMO(mo);
        //PLS
        PLS pls = new PLS();
        pls.setID(0);                       //生产线日计划id
        pls.setCode("");                    //生产线日计划编码
        pls.setName("");                    //生产线日计划名称
        compRptDatas.setPLS(pls);
        //WhshipmentReason
        compRptDatas.setWhshipmentReason(0);            //退库理由
        //ProductBaseUOM
        ProductBaseUOM productBaseUOM = new ProductBaseUOM();
        productBaseUOM.setID(0);                //生产副单位id
        productBaseUOM.setCode("");             //生产副单位编码
        productBaseUOM.setName("");             //生产副单位名称
        compRptDatas.setProductBaseUOM(productBaseUOM);
        //ProductUOM
        ProductUOM productUOM = new ProductUOM();
        productUOM.setID(0);                    //生产单位id
        productUOM.setCode("");                 //生产单位编码
        productUOM.setName(recpt.getUnitOfMeasure());       //生产单位名称
        compRptDatas.setProductUOM(productUOM);
        //PUToPBURate
        compRptDatas.setPUToPBURate(0);             //生产主-生产副
        //CompleteQty
        compRptDatas.setCompleteQty(recpt.getQuantityRecpt());   //完工数量
        //CompleteQtyCoUOM
        compRptDatas.setCompleteQtyCoUOM(recpt.getQuantityRecpt());  //完工数量
        //CompleteQtyWhUOM
        compRptDatas.setCompleteQtyWhUOM(recpt.getQuantityRecpt());  //完工数量
        //EligibleQtyCoUOM
        compRptDatas.setEligibleQtyCoUOM(recpt.getQuantityQualified());  //合格数量
        //ReworkingQtyCoUOM
        compRptDatas.setReworkingQtyCoUOM(recpt.getQuantityRecpt().subtract(recpt.getQuantityQualified()));  //待返工数量
        //ScrapQtyCoUOM
        compRptDatas.setScrapQtyCoUOM(recpt.getQuantityUnquanlified());  //报废数量
        //EligibleQty
        compRptDatas.setEligibleQty(recpt.getQuantityQualified());  //合格数量
        //ReworkingQty
        compRptDatas.setReworkingQty(recpt.getQuantityRecpt().subtract(recpt.getQuantityQualified()));    //待返工数量
        //ScrapQty
        compRptDatas.setScrapQty(recpt.getQuantityUnquanlified());            //报废数量
        //ReworkReason
        compRptDatas.setReworkReason(0);    //返工原因
        //ScrapReason
        compRptDatas.setScrapReason(0);   // 报废原因
        //BusinessDate
        compRptDatas.setBusinessDate(new Date().toString());
        //ActualRcvTime
        compRptDatas.setActualRcvTime(new Date());   //实际入库时间
        //RcvWh
        RcvWh rcvWh = new RcvWh();
        rcvWh.setID(recpt.getWarehouseId());               //存储地点id     //类型转换
        rcvWh.setCode(recpt.getWarehouseCode());            // 存储地点编码
        rcvWh.setName(recpt.getWarehouseName());             //存储地点
        compRptDatas.setRcvWh(rcvWh);
        //RcvOrg
        RcvOrg rcvOrg = new RcvOrg();
        rcvOrg.setID(1002401113575156L);             //入库组织
        rcvOrg.setCode("001");         //入库组织
        rcvOrg.setName("山东鲁银新材料科技有限公司");         //入库组织
        compRptDatas.setRcvOrg(rcvOrg);
        //CompleteRptRcvLines
        List<CompleteRptRcvLines> completeRptRcvLinesList = new ArrayList<>();
//        CompleteRptRcvLines completeRptRcvLines = new CompleteRptRcvLines();
        //CompleteRpt
        CompleteRpt completeRpt = new CompleteRpt();
        completeRpt.setID(0);
        completeRpt.setCode("");
        completeRpt.setName("");
        //SourceCompleteRptRcv
        SourceCompleteRptRcv sourceCompleteRptRcv = new SourceCompleteRptRcv();
        sourceCompleteRptRcv.setID(recpt.getWorkorderId());          //来源完工报告
        sourceCompleteRptRcv.setCode(recpt.getWorkorderCode());
        sourceCompleteRptRcv.setName(recpt.getWorkorderName());
        //SourceCompleteRptRcvLine
        SourceCompleteRptRcvLine sourceCompleteRptRcvLine = new SourceCompleteRptRcvLine();
        sourceCompleteRptRcvLine.setID(0);          //来源完工报告入库子表
        sourceCompleteRptRcvLine.setCode("");
        sourceCompleteRptRcvLine.setName("");
        //Wh
        Wh wh = new Wh();
        wh.setID(recpt.getWarehouseId());                   //类型转换
        wh.setCode(recpt.getWarehouseCode());
        wh.setName(recpt.getWarehouseName());
        //Bin
        Bin bin = new Bin();
        bin.setID(0);           //  库位
        bin.setCode("");
        bin.setName("");
        //CompleteRptRcvLines
        CompleteRptRcvLines completeRptRcvLines = new CompleteRptRcvLines();
        completeRptRcvLines.setLineNum(10);         //  行号
        completeRptRcvLines.setGrade(2011);           //  等级
        completeRptRcvLines.setStorageType(4);          //  	存储类型
        completeRptRcvLines.setElement(0);              //成分
        completeRptRcvLines.setCostPercent(0);          //成本比例
        completeRptRcvLines.setRcvLotMaster(0);         //入库批号主档
        completeRptRcvLines.setRcvLotNo(autoCodeUtil.genSerialCode(UserConstants.COMPRPTDATA_CODE,""));    //入库批号
        completeRptRcvLines.setRcvQtyByProductUOM(recpt.getQuantityRecpt());   //生产单位
        completeRptRcvLines.setRcvQtyByWhUOM(recpt.getQuantityRecpt());        //库存单位
        completeRptRcvLines.setRcvQtyByCostUOM(recpt.getQuantityRecpt());     // 成本单位
        completeRptRcvLines.setCUD(0);
        completeRptRcvLines.setRemark("");
        completeRptRcvLines.setRcvLotEffectivedate("");     //入库批号生效期
        completeRptRcvLines.setRcvValiddate(0);             //	入库批号有效期天数
        completeRptRcvLines.setCompleteRpt(completeRpt);
        completeRptRcvLines.setSourceCompleteRptRcv(sourceCompleteRptRcv);
        completeRptRcvLines.setSourceCompleteRptRcvLine(sourceCompleteRptRcvLine);
        completeRptRcvLines.setWh(wh);
        completeRptRcvLines.setBin(bin);
        completeRptRcvLinesList.add(completeRptRcvLines);

        compRptDatas.setCompleteRptRcvLines(completeRptRcvLinesList);

        //OwnerOrg
        OwnerOrg ownerOrg = new OwnerOrg();
        ownerOrg.setID(1002401113575156L);              //  货主组织id
        ownerOrg.setCode("001");           //  货主组织编码
        ownerOrg.setName("山东鲁银新材料科技有限公司");           //  货主组织名称
        compRptDatas.setOwnerOrg(ownerOrg);
        //ItemMaster
        ItemMaster itemMaster = new ItemMaster();
        itemMaster.setID(recpt.getItemId());           //料品id             //类型转换
        itemMaster.setCode(recpt.getItemCode());       //料品编码
        itemMaster.setName(recpt.getItemName());       //料品名称
        compRptDatas.setItemMaster(itemMaster);
        //IsSpecial
        compRptDatas.setIsSpecial(true);
        //DocStatus
        compRptDatas.setDocStatus(2);

        //        System.out.println(jsonObject);
        return erpService.proFeedbackToErp(compRptDatas);
    }
}
